package com.atguigu.tree;

import com.atguigu.map.BSTMap;
import com.atguigu.set.FileOperation;

import java.util.ArrayList;


public class AVLTreeTest {

    public static void main(String[] args) {
        System.out.println("Pride and Prejudice");
        ArrayList<String> words = new ArrayList<>();
        String filename = "pride-and-prejudice.txt";
        if (FileOperation.readFile(filename, words)) {
            System.out.println("Total words：" + words.size());
            System.out.println("==================================================");
            long startTime = System.nanoTime();
            BSTMap<String, Integer> bstMap = new BSTMap<>();
            for (String word : words) {
                Integer count = bstMap.get(word);
                bstMap.add(word, count == null ? 1 : count + 1);
            }
            for(String word:words){
                bstMap.contains(word);
            }
            System.out.println("BSTree: "+bstMap.getSize());
            long endTime = System.nanoTime();
            double time=(endTime-startTime)/1000000000.0;
            System.out.println("BST: "+time+" s");
            System.out.println("==================================================");


            long startTime1 = System.nanoTime();
            AVLTree<String,Integer>avlTree=new AVLTree<>();
            for(String word:words){
                if(avlTree.contains(word)){
                    avlTree.set(word,avlTree.get(word)+1);
                }else{
                    avlTree.add(word,1);
                }
            }
            for(String word:words){
                avlTree.contains(word);
            }
            System.out.println("AVLTree:  "+avlTree.getSize());
            long endTime1 = System.nanoTime();
            double time1=(endTime1-startTime1)/1000000000.0;
            System.out.println("AVLTree: "+time1+" s");
            System.out.println("==================================================");

            long startTime2 = System.nanoTime();
            RBTree<String,Integer>brt=new RBTree<>();
            for(String word:words){
                if(brt.contains(word)){
                    brt.set(word,brt.get(word)+1);
                }else{
                    brt.add(word,1);
                }
            }
            for(String word:words){
                brt.contains(word);
            }
            System.out.println("RBTree:  "+brt.getSize());
            long endTime2 = System.nanoTime();
            double time2=(endTime2-startTime2)/1000000000.0;
            System.out.println("RBTree: "+time2+" s");
        }
    }
}